package com.leech.jcrop.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.leech.jcrop.util.ImageUtils;

public class JcropServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public JcropServlet() {
       
    }
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		int x1=Integer.parseInt(request.getParameter("x1"));
		int y1=Integer.parseInt(request.getParameter("y1"));
		int x2=Integer.parseInt(request.getParameter("x2"));
		int y2=Integer.parseInt(request.getParameter("y2"));
		
		String rootPath = request.getSession().getServletContext().getRealPath("/");
		
		String src=rootPath+File.separator+"resources"+File.separator+"fj.jpg";
		String dest=rootPath+File.separator+"resources"+File.separator+"fj_crop.jpg";
		
		ImageUtils.cut(src, dest, x1, y1, x2, y2);
		
		PrintWriter out=response.getWriter();
		out.print("fj_crop.jpg");
       
	}

}
